//
//  SSDKBaseResData.m
//  SuperSDK
//
//  Created by wyht－ios－dev on 15/10/7.
//  Copyright © 2015年 WYHT. All rights reserved.
//

#import "SSDKBaseResData.h"
#import "SSDKDataKeys.h"
#import "SuperSDKPluginTools/SSDKPluginJsonUtil.h"


@implementation SSDKBaseResData

NSString * const STATUS_OK = @"ok";

NSString * const STATUS_FAIL = @"fail";

- (instancetype) init {
    self = [super init];
    
    if (self) {
        self.status = @"";
        self.data = [[NSMutableDictionary alloc] init];
        self.common = [[NSMutableDictionary alloc] init];
        self.extra = [[NSMutableDictionary alloc] init];
    }
    
    return self;
}

- (instancetype) initWithDictionary:(NSMutableDictionary *)dictionary {
    self = [super init];
    
    if (self) {
        if ([dictionary valueForKey:STATUS]) {
            self.status = [dictionary valueForKey:STATUS];
        }
        
        if ([dictionary valueForKey:DATA]) {
            self.data = [dictionary valueForKey:DATA];
        }
        
        if ([dictionary valueForKey:COMMON]) {
            self.common = [dictionary valueForKey:COMMON];
        }
        
        if ([dictionary valueForKey:EXTRA]) {
            self.extra = [dictionary valueForKey:EXTRA];
        }
    }
    
    return self;
}

- (BOOL) isOk {
    if ([STATUS_OK isEqualToString: self.status]) {
        return YES;
    }
    return NO;
}

- (instancetype) fail {
    self.status = STATUS_FAIL;
    return self;
}

- (instancetype) ok {
    self.status = STATUS_OK;
    return self;
}

- (instancetype) setError : (NSString *) error
                 withCode : (NSInteger) errCode {
    NSString *code = [NSString stringWithFormat:@"%ld", (long)errCode];
    
    [self.data setValue:error forKey:ERROR];
    [self.data setValue:code forKey:ERROR_NO];
    [self.data setValue:@"" forKey:ERROR_MSG];
    return self;
}

- (instancetype) setError : (NSString *) error
                 withCode : (NSInteger) errCode
                  withMsg : (NSString *) msg {
    NSString *code = [NSString stringWithFormat:@"%ld", (long)errCode];
    
    [self.data setValue:error forKey:ERROR];
    [self.data setValue:code forKey:ERROR_NO];
    [self.data setValue:msg forKey:ERROR_MSG];
    return self;
}

- (NSString *) appId {
    NSString *value = [self.data valueForKey:APP_ID];
    if (!value) {
        return @"";
    }
    return value;
}

- (instancetype) setAppId:(NSString *)appId {
    [self.data setValue:appId forKey:APP_ID];
    return self;
}

- (NSString *) sdkId {
    NSString *value = [self.data valueForKey:SDK_ID];
    if (!value) {
        return @"";
    }
    return value;
}

- (instancetype) setSdkId:(NSString *)sdkId {
    [self.data setValue:sdkId forKey:SDK_ID];
    return self;
}

- (NSString *) channelId {
    NSString *value = [self.data valueForKey:CHANNEL_ID];
    if (!value) {
        return @"";
    }
    return value;
}

- (instancetype) setChannelId:(NSString *)channelId {
    [self.data setValue:channelId forKey:CHANNEL_ID];
    return self;
}

- (NSString *) error {
    NSString *value = [self.data valueForKey:ERROR];
    if (!value) {
        return @"";
    }
    return value;
}

- (int) errorNo {
    NSString *value = [self.data valueForKey:ERROR_NO];
    if (!value) {
        return 0;
    }
    return [value intValue];
}

- (NSString *) errorMsg {
    NSString *value = [self.data valueForKey:ERROR_MSG];
    if (!value) {
        return @"";
    }
    return value;
}

- (instancetype) setErrorMsg:(NSString *)msg {
    [self.data setValue:msg forKey:ERROR_MSG];
    return self;
}

- (NSString *) dataWithKey:(NSString *)key {
    NSString *value = [self.data valueForKey:key];
    if (!value) {
        return @"";
    }
    return value;
}

- (instancetype) setDataValue:(NSString *)value
                       forKey:(NSString *)key {
    [self.data setValue:value forKey:key];
    return self;
}

- (NSString *) extraWithKey:(NSString *)key {
    NSString *value = [self.extra valueForKey:key];
    if (!value) {
        return @"";
    }
    return value;
}

- (instancetype) setExtraValue:(NSString *)value
                        forKey:(NSString *)key {
    [self.extra setValue:value forKey:key];
    return self;
}

- (NSString *) toJson {
    NSMutableDictionary *jsonDic = [[NSMutableDictionary alloc] init];
    
    [jsonDic setValue:self.status forKey:STATUS];
    [jsonDic setValue:self.data forKey:DATA];
    [jsonDic setValue:self.common forKey:COMMON];
    [jsonDic setValue:self.extra forKey:EXTRA];
    return [SSDKPluginJsonUtil toJsonWithObject:jsonDic];
}

@end
